package com.yuncheng.spcyApi.selectXzxkApi.api;

import com.alibaba.fastjson.JSONObject;
import com.yuncheng.spcyApi.constant.zwfw.ApiConstant;
import com.yuncheng.spcyApi.selectXzxkApi.bo.XzspQyRcjcBo;
import com.yuncheng.spcyApi.selectXzxkApi.bo.XzspQyzjxxBo;
import com.yuncheng.spcyApi.spcyService.SpcyQyInfoService;
import com.yuncheng.spcyApi.utils.HttpProxyUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.vo.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hua
 */
@Component(value = "xzspQyApi")
public class XzspQyApi {

    private static final Logger log = LoggerFactory.getLogger(XzspQyApi.class);

    @Resource
    private ApiConstant apiConstant;

    @Resource
    @Lazy
    private XzspJkApi xzspJkApi;

    /**
     * 9.获取企业证件信息接口
     * @param tyshxydm 统一社会信用代码
     * @param qylbbm 企业类别编码
     */
    public HttpResult selectQyzj(String tyshxydm, String qylbbm) {

        String url = apiConstant.hqqyzj;
        String token = "\"" + xzspJkApi.getToken() + "\"";
        String jsParam = "{" +
                "\"access_token\": " + token + "," +
                "\"param\":{" +
                "\"tyshxydm\":\"" + tyshxydm + "\"," +
                "\"qylbbm\":\"" + qylbbm + "\"" +
                "}}";

        String resString = HttpProxyUtils.HttpProxyPost(url, jsParam);
        if (resString == null) {
            return HttpResult.error("接口请求异常！请联系管理员！");
        }

        JSONObject resJson = JSONObject.parseObject(resString);
        JSONObject status = resJson.getJSONObject("STATUS");
        if (status.getString("CODE").equals("00")) {

            JSONObject data = resJson.getJSONObject("data");
            List<XzspQyzjxxBo> list = null;
            if (data.containsKey("LIST")) {
                list = JSONObject.parseArray(data.get("LIST").toString(), XzspQyzjxxBo.class);
            }else if (data.containsKey("list")){
                list = JSONObject.parseArray(data.get("list").toString(), XzspQyzjxxBo.class);
            }

            return HttpResultVo.HttpResultList(list);
        } else {
            return HttpResult.error(status.getString("TEXT"));
        }

    }

    /**
     * 10.获取证件详细信息接口
     */
    public HttpResult selectQyzjxx(String zjid){
        String url = apiConstant.getZjxxxx;
        String token = "\"" + xzspJkApi.getToken() + "\"";
        String jsParam = "{" +
                "\"access_token\": " + token + "," +
                "\"param\":{" +
                "\"zjid\":\"" + zjid + "\"" +
                "}}";

        String resString = HttpProxyUtils.HttpProxyPost(url, jsParam);
        if (resString == null) {
            return HttpResult.error("接口请求异常！请联系管理员！");
        }

        JSONObject resJson = JSONObject.parseObject(resString);
        JSONObject status = resJson.getJSONObject("STATUS");
        if (status.getString("CODE").equals("00")) {

            if (resJson.containsKey("data")){
                JSONObject data = resJson.getJSONObject("data");
//                XzspQyYpscxkzxxBo bean = null;
                return HttpResult.ok(data);
            }

            return HttpResult.ok(new JSONObject());
        } else {
            return HttpResult.error(status.getString("TEXT"));
        }
    }

    /**
     * 11.获取日常检查信息接口
     */
    public HttpResult selectQyRcjc(String tyshxydm){
        String url = apiConstant.hqrcjc;
        String token = "\"" + xzspJkApi.getToken() + "\"";
        String jsParam = "{" +
                "\"access_token\": " + token + "," +
                "\"param\":{" +
                "\"tyshxydm\":\"" + tyshxydm + "\"" +
                "}}";

        String resString = HttpProxyUtils.HttpProxyPost(url, jsParam);
        if (resString == null) {
            return HttpResult.error("接口请求异常！请联系管理员！");
        }

        JSONObject resJson = JSONObject.parseObject(resString);
        JSONObject status = resJson.getJSONObject("STATUS");
        if (status.getString("CODE").equals("00")) {

            JSONObject data = resJson.getJSONObject("data");
            List<XzspQyRcjcBo> list = null;
            if (data.containsKey("list")) {
                list = JSONObject.parseArray(data.get("list").toString(), XzspQyRcjcBo.class);
            }else if (data.containsKey("LIST")){
                list = JSONObject.parseArray(data.get("LIST").toString(), XzspQyRcjcBo.class);
            }

            return HttpResultVo.HttpResultList(list);
        } else {
            return HttpResult.error(status.getString("TEXT"));
        }
    }

}
